<?php
declare (strict_types=1);

namespace app\store\controller\student;

use app\store\controller\Controller;
use app\store\model\store\User as StoreUserModel;
use app\store\model\Student as StudentModel;
use think\db\exception\DbException;

class Index extends Controller
{
    public function createUser()
    {
        $data = $this->postData();
        $model = new StudentModel;
        $where = [
            'name' => $data['user_name'],
//            'phone' => $data['phone'],
            'idnumber' => $data['idcard'],
            'score' => $data['score'] ?? 0,
            'job' => $data['subject_name'],
            'subject_level' => $data['level_name'],
//            'channel_id' => $data['channel_id'],
            'store_id' => 10001,
            'is_cancel' => 0
        ];
        $student = $model->where($where)->where("is_delete", "=", 0)->find();
        if ($student) {
            return $this->renderJson(200, "您已参加过考试，如有疑问请联系对应机构");
        } else {
            $savedata = $where;
            $savedata['phone'] = $data['phone'];
            $savedata['avatar_url'] = $data['exam_url'];
            $savedata['sign_url'] = $data['sign_url'];
            $savedata['channel_id'] = $data['channel_id'] ?: 10001;
            $model->saveData($savedata);
        }
        return $this->renderSuccess("成功");
    }

    public function byFindStudent()
    {
        var_dump(time());
    }

    /**
     * 管理员列表
     * @return array
     * @throws DbException
     */
    public function list()
    {
        $model = new StudentModel;
        $param = $this->request->param() ?: [];
        $studentUsermodel = new StoreUserModel;
        $param['channel_id'] = $studentUsermodel->getChildrenUserIds(!empty($param['channel_id']) ? $param['channel_id'] : $this->store['user']['store_user_id']);
        $list = $model->getList($param);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 删除学员
     * @param int $studentId
     * @return array|bool
     */
    public function delete(int $studentId)
    {
        // 管理员详情
        $model = StudentModel::detail($studentId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 更新学员
     * @param int $studentId
     * @return array
     */
    public function edit(int $studentId)
    {
        // 管理员详情
        $model = StudentModel::detail($studentId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }


    public function exportAvatar()
    {
        $model = new StudentModel;
        $param = $this->postData();

        $studentUsermodel = new StoreUserModel;
        $param['channel_id'] = $studentUsermodel->getChildrenUserIds(!empty($param['channel_id']) ? $param['channel_id'] : $this->store['user']['store_user_id']);

        $list = $model->exportAvatar($param);
        if (!$list) {
            return $this->renderError($model->getError());
        }
        return $this->renderSuccess($list);
    }


}
